package models

import (
	"github.com/myaccount/my-app/app/db"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"time"
)

type ICard interface {
	Add(card Card) error
	RmId(id bson.ObjectId) error
	GetId(id bson.ObjectId, obj interface{}) error
	ShieldCard(id bson.ObjectId, reasons []string) error
	GetCardsByQid(qid string, limit int, obj interface{}) error
	GetCardsByTs(cardType, limit int, obj interface{}) error
	GetCardsByFavor(cardType, limit int, obj interface{}) error
	GetCardsByTrend(cardType, limit int, obj interface{}) error
	GetAllCards(cardType, limit int, obj interface{}) error
}

type Shield struct {
	isShield bool     `bson:"isShield" json:"isShield" valid:"Required"`
	reason   []string `bson:"reason" json:"reason" valid:"Required"`
	qid      string   `bson:"qid" json:"qid" valid:"Required"`
}

type Card struct {
	Id          bson.ObjectId `bson:"_id" json:"cardId"`
	Author      User          `bson:"author" json:"author"`
	Title       string        `bson:"title" json:"title" valid:"Required"`
	Description string        `bson:"description" json:"description" valid:"Required"`
	Type        int           `bson:"type" json:"type" valid:"Required"`
	Contents    []Content     `bson:"contents" json:"contents" valid:"Required"`
	Shield      Shield        `bson:"shield" json:"shield"`
	Created     int64         `bson:"created" json:"created"`
}

func (this *Card) do(f func(*mgo.Collection) error) error {
	return db.DO("card", "card", f)
}

func (this *Card) Add(card Card) error {
	card.Id = bson.NewObjectId()
	card.Created = time.Now().Unix()
	return this.do(func(c *mgo.Collection) error {
		return c.Insert(card)
	})
}

func (this *Card) RmId(id bson.ObjectId) error {
	return this.do(func(c *mgo.Collection) error {
		return c.RemoveId(id)
	})
}

func (this *Card) GetId(id bson.ObjectId, obj interface{}) error {
	return this.do(func(c *mgo.Collection) error {
		return c.FindId(id).One(obj)
	})
}

func (this *Card) ShieldCard(id bson.ObjectId, qid string, reasons []string) error {
	return this.do(func(c *mgo.Collection) error {
		return c.UpdateId(id, bson.M{"$set": bson.M{
			"shield": bson.M{"isShield": true, "reason": reasons, "qid": qid}}})
	})
}

// TODO: page
func (this *Card) GetCardsByQid(qid string, limit int, obj interface{}) error {
	return this.do(func(c *mgo.Collection) error {
		return c.Find(bson.M{"author.qid": qid}).All(obj)
	})
}

// TODO: page
func (this *Card) GetCardsByTs(cardType, obj interface{}) error {
	//		return this.do(func(c *mgo.Collection) error{
	//		return c.Find(bson.M{"author.qid":qid}).All(obj)
	//	})
	return nil
}

func (this *Card) GetCardsByFavor(cardType, obj interface{}) error {
	//		return this.do(func(c *mgo.Collection) error{
	//		return c.Find(bson.M{"author.qid":qid}).All(obj)
	//	})
	return nil
}

func (this *Card) GetCardsByTrend(cardType, obj interface{}) error {
	//		return this.do(func(c *mgo.Collection) error{
	//		return c.Find(bson.M{"author.qid":qid}).All(obj)
	//	})
	return nil
}

func (this *Card) GetAllCards(cardType, limit int, obj interface{}) error {
	return nil
}
